<?php

class users extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('users_model');
    }

    function index($page = 1, $per_page = 20) {
        //$this->data['total'] = 100;
		$this->users_model->lists($page, $per_page);
        $this->load->view('users/index.php', $this->data);
    }
	
	function add() {
		if (!$this->input->post()) {
			$this->load->view('/users/add.php');
		} else {
			$name = $this->input->post('name');
			$account = $this->input->post('account');
			$password = $this->input->post('password');
            $phone = $this->input->post('phone');
			
			$data = array(
				'name' => $name,
				'password' => $password,
				'account' => $account,
				'phone' => $phone
			);
			$this->users_model->add($data);
			redirect(base_url('/users'));
		}
	}
	
	function edit($id) {
		if (!$this->input->post()) {
			$user = $this->users_model->get($id);
			$this->data['user'] = $user[0];
			$this->load->view('users/edit.php', $this->data);
		} else {
			$name = $this->input->post('name');
			$account = $this->input->post('account');
			$password = $this->input->post('password');
            $phone = $this->input->post('phone');
			
			$data = array(
				'name' => $name,
				'password' => $password,
				'account' => $account,
                'phone' => $phone
			);
			$this->users_model->update($id, $data);
			redirect(base_url('/users'));
		}
	}
	
	function delete($id) {
		$this->users_model->delete($id);
	}
}